import {createAction}       from 'redux-actions';
import {serverError}        from './system';
import api, {SessionUser}   from '../api';

export const fetchSession = createAction('@@user/FETCH_SESSION');
export const fetchProfile = createAction('@@user/FETCH_PROFILE');


let user = new SessionUser();

export function fetchCurrentFromServer() {
  return function (dispatch) {
    user.fetch().catch(() => {
      dispatch(serverError("Server Error"));
    });
    user.one('set', () => {
      dispatch(fetchSession(user))
    });
  }
}


export function fetchProfileFromServer() {
  return function (dispatch) {
    api.getProfile().then(function (data) {
      dispatch(fetchProfile(data));
    });
  }
}